package com.ruicar.afs.cloud.afscase.archive.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.approvemakelabel.entity.ApproveMakeLabel;
import com.ruicar.afs.cloud.afscase.approvemakelabel.entity.LoanLabelInfo;
import com.ruicar.afs.cloud.afscase.approvemakelabel.service.ApproveMakeLabelService;
import com.ruicar.afs.cloud.afscase.approvemakelabel.service.LoanLabelInfoService;
import com.ruicar.afs.cloud.afscase.archive.dto.ArchiveApiDto;
import com.ruicar.afs.cloud.afscase.archive.service.ArchiveApiService;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.common.modules.contract.enums.YesOrNoEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: fangchenliang
 * @Date: 2020/8/23 16:59
 */
@Slf4j
@Api("归档回调接口")
@RestController
@RequestMapping("/archive")
@AllArgsConstructor
public class ArchiveApiController {

    private final ArchiveApiService archiveApiService;
    private final ApproveMakeLabelService approveMakeLabelService;
    private final LoanLabelInfoService loanLabelInfoService;
    private final CaseContractInfoService caseContractInfoService;

    @PostMapping(value = "/releaseQuota")
    @ApiOperation("额度释放")
    public JSONObject releaseQuota(@RequestBody ArchiveApiDto quotaJson){
        JSONObject jsonObject=new JSONObject();
        try{
            return archiveApiService.releaseQuota(quotaJson);
        }catch (Exception e){
            log.error("{}额度释放失败：{}",quotaJson.getContractNo()==null?quotaJson.getContractId() : quotaJson.getContractNo() ,e);
            jsonObject.put("success",0);
            jsonObject.put("code","0");
            jsonObject.put("msg","失败");
            return jsonObject;
        }
    }

    @PostMapping(value = "/removeOverdue")
    @ApiOperation("合作商超期解除")
    public JSONObject removeOverdue(@RequestBody ArchiveApiDto overdueJson){
        String dataId =overdueJson.getDataId();
        JSONObject jsonObject=new JSONObject();
        try{
            return archiveApiService.removeOverdue(dataId);
        }catch (Exception e){
            log.error("{}合作商超期解除失败：{}",dataId,e);
            jsonObject.put("success",0);
            jsonObject.put("code","0");
            jsonObject.put("msg","失败");
            return jsonObject;
        }
    }

    @PostMapping(value = "/getTagInfo")
    @ApiOperation("获取标签信息")
    public JSONObject getTagInfo(@RequestBody ArchiveApiDto quotaJson){
        String dataId =quotaJson.getDataId();
        JSONObject jsonObject=new JSONObject();
        try{
            List result = new ArrayList();
            String[] split = dataId.split(",");
            for (int i = 0; i < split.length; i++) {
                JSONObject json=new JSONObject();
                //获取申请编号
                String applyNo = split[i];
                //标签信息
                List<String> tagNameList = new ArrayList<>();
                CaseContractInfo caseContractInfo = caseContractInfoService.getOne(Wrappers.<CaseContractInfo>query().lambda()
                        .eq(CaseContractInfo::getApplyNo,applyNo)
                );
                //根据申请编号查询标签信息
                List<LoanLabelInfo> loanLabelInfos = loanLabelInfoService.list(Wrappers.<LoanLabelInfo>query().lambda()
                        .eq(LoanLabelInfo::getApplyNo,applyNo)
                        .eq(LoanLabelInfo::getIsArchive,YesOrNoEnum.yes)
                );
                for(LoanLabelInfo loanLabelInfo:loanLabelInfos){
                    tagNameList.add(loanLabelInfo.getLabelName());
                }
                //根据申请编号查询标签信息
                List<ApproveMakeLabel> approveMakeLabelList = approveMakeLabelService.list(Wrappers.<ApproveMakeLabel>query().lambda()
                        .eq(ApproveMakeLabel::getApplyNo,applyNo)
                        .eq(ApproveMakeLabel::getIsArchive,YesOrNoEnum.yes)
                );
                for(ApproveMakeLabel approveMakeLabel:approveMakeLabelList){
                    tagNameList.add(approveMakeLabel.getLabelName());
                }
                //去除重复数据
                HashSet h = new HashSet(tagNameList);
                tagNameList.clear();
                tagNameList.addAll(h);
                //List转变为逗号分隔的String
                String tagName = tagNameList.stream().collect(Collectors.joining(","));
                //汇总结果
                json.put("camainNo",applyNo);
                json.put("tagName",tagName);
                result.add(json);
            }
            jsonObject.put("data",result);
            jsonObject.put("success",1);
            return jsonObject;
        }catch (Exception e){
            log.error("{}获取标签信息失败：{}",dataId,e);
            jsonObject.put("success",0);
            jsonObject.put("code","0");
            jsonObject.put("msg","失败");
            return jsonObject;
        }
    }


}
